package api

import (
	"file_sys_server/serializer"
	"file_sys_server/service"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

/**
	添加新的校准记录
**/
func AddChannle1Calibration_MFDCG(ctx *gin.Context) {
	var service service.Channel1RegionService // 新的校准记录
	response := &Response{0, ""}

	//bind new Record
	if err := ctx.BindJSON(&service); err != nil { // 获取
		fmt.Println(err.Error())
		response.Code = 10001
		response.Msg = "请求错误"
		ctx.IndentedJSON(http.StatusBadRequest, response)
		return
	}
	res := service.AddRecord() // 存储数据
	// res := service.addRecord()
	ctx.IndentedJSON(http.StatusCreated, res)

}

/**
	添加新的校准记录通道2
**/
func AddChannle2Calibration_MFDCG(ctx *gin.Context) {
	var service service.Channel2RegionService // 新的校准记录
	response := &Response{0, ""}

	//bind new Record
	if err := ctx.BindJSON(&service); err != nil { // 获取
		fmt.Println(err.Error())
		response.Code = 10001
		response.Msg = "请求错误"
		ctx.IndentedJSON(http.StatusBadRequest, response)
		return
	}
	res := service.AddRecord() // 存储数据
	// res := service.addRecord()
	ctx.IndentedJSON(http.StatusCreated, res)
}

/**
	添加新的校准记录通道3
**/
func AddChannle3Calibration_MFDCG(ctx *gin.Context) {
	var service service.Channel3RegionService // 新的校准记录
	response := &Response{0, ""}

	//bind new Record
	if err := ctx.BindJSON(&service); err != nil { // 获取
		fmt.Println(err.Error())
		response.Code = 10001
		response.Msg = "请求错误"
		ctx.IndentedJSON(http.StatusBadRequest, response)
		return
	}
	res := service.AddRecord() // 存储数据
	// res := service.addRecord()
	ctx.IndentedJSON(http.StatusCreated, res)
}

/**
	添加新的校准记录通道4
**/
func AddChannle4Calibration_MFDCG(ctx *gin.Context) {
	var service service.Channel4RegionService // 新的校准记录
	response := &Response{0, ""}

	//bind new Record
	if err := ctx.BindJSON(&service); err != nil { // 获取
		fmt.Println(err.Error())
		response.Code = 10001
		response.Msg = "请求错误"
		ctx.IndentedJSON(http.StatusBadRequest, response)
		return
	}
	res := service.AddRecord() // 存储数据
	// res := service.addRecord()
	ctx.IndentedJSON(http.StatusCreated, res)
}

/**
	添加新的校准记录通道 左侧单卡
**/
func AddChannle1SpecCalibration_MFDCG(ctx *gin.Context) {
	var service service.Channel1SpecRegionService // 新的校准记录
	response := &Response{0, ""}

	//bind new Record
	if err := ctx.BindJSON(&service); err != nil { // 获取
		fmt.Println(err.Error())
		response.Code = 10001
		response.Msg = "请求错误"
		ctx.IndentedJSON(http.StatusBadRequest, response)
		return
	}
	res := service.AddRecord() // 存储数据
	// res := service.addRecord()
	ctx.IndentedJSON(http.StatusCreated, res)
}

/**
	添加新的校准记录通道 裸条
**/
func AddChannleSpecCalibration_MFDCG(ctx *gin.Context) {
	var service service.ChannelSpecRegionService // 新的校准记录
	response := &Response{0, ""}

	//bind new Record
	if err := ctx.BindJSON(&service); err != nil { // 获取
		fmt.Println(err.Error())
		response.Code = 10001
		response.Msg = "请求错误"
		ctx.IndentedJSON(http.StatusBadRequest, response)
		return
	}
	res := service.AddRecord() // 存储数据
	// res := service.addRecord()
	ctx.IndentedJSON(http.StatusCreated, res)
}

// 获取数据库中存储1卡校准数据
func GetChannel1_MFDCG(c *gin.Context) {
	calibration, err := service.GetChannel1Record()
	if err != nil {
		c.JSON(http.StatusOK, &Response{Code: serializer.DatabaseErr, Msg: "数据库操作异常" + err.Error()})
		fmt.Println(err.Error())
		return
	}
	response := service.Channel1RegionService{
		Top1:      calibration.Top1,
		Left1:     calibration.Left1,
		Right1:    calibration.Right1,
		Bottom1:   calibration.Bottom1,
		BgFactors: calibration.BgFactors,
	}

	c.IndentedJSON(http.StatusOK, response)
}

// 获取数据库中存储左1卡校准数据
func GetChannel1Spec_MFDCG(c *gin.Context) {
	calibration, err := service.GetChannel1SpecRecord()
	if err != nil {
		c.JSON(http.StatusOK, &Response{Code: serializer.DatabaseErr, Msg: "数据库操作异常" + err.Error()})
		fmt.Println(err.Error())
		return
	}
	response := service.Channel1SpecRegionService{
		Top1:      calibration.Top1,
		Left1:     calibration.Left1,
		Right1:    calibration.Right1,
		Bottom1:   calibration.Bottom1,
		BgFactors: calibration.BgFactors,
	}

	c.IndentedJSON(http.StatusOK, response)
}

// 获取数据库中存储左1卡校准数据
func GetChannelSpec_MFDCG(c *gin.Context) {
	calibration, err := service.GetChannelSpecRecord()
	if err != nil {
		c.JSON(http.StatusOK, &Response{Code: serializer.DatabaseErr, Msg: "数据库操作异常" + err.Error()})
		fmt.Println(err.Error())
		return
	}
	response := service.ChannelSpecRegionService{
		Top1:      calibration.Top1,
		Left1:     calibration.Left1,
		Right1:    calibration.Right1,
		Bottom1:   calibration.Bottom1,
		BgFactors: calibration.BgFactors,
	}

	c.IndentedJSON(http.StatusOK, response)
}

// 获取数据库中存储2卡校准数据
func GetChannel2_MFDCG(c *gin.Context) {
	calibration, err := service.GetChannel2Record()
	if err != nil {
		c.JSON(http.StatusOK, &Response{Code: serializer.DatabaseErr, Msg: "数据库操作异常" + err.Error()})
		fmt.Println(err.Error())
		return
	}
	response := service.Channel2RegionService{
		Top1:      calibration.Top1,
		Left1:     calibration.Left1,
		Right1:    calibration.Right1,
		Bottom1:   calibration.Bottom1,
		BgFactors: calibration.BgFactors,

		Top2:       calibration.Top2,
		Left2:      calibration.Left2,
		Right2:     calibration.Right2,
		Bottom2:    calibration.Bottom2,
		BgFactors2: calibration.BgFactors2,
	}

	c.IndentedJSON(http.StatusOK, response)
}

// 获取数据库中存储3卡校准数据
func GetChannel3_MFDCG(c *gin.Context) {
	calibration, err := service.GetChannel3Record()
	if err != nil {
		c.JSON(http.StatusOK, &Response{Code: serializer.DatabaseErr, Msg: "数据库操作异常" + err.Error()})
		fmt.Println(err.Error())
		return
	}
	response := service.Channel3RegionService{
		Top1:      calibration.Top1,
		Left1:     calibration.Left1,
		Right1:    calibration.Right1,
		Bottom1:   calibration.Bottom1,
		BgFactors: calibration.BgFactors,

		Top2:       calibration.Top2,
		Left2:      calibration.Left2,
		Right2:     calibration.Right2,
		Bottom2:    calibration.Bottom2,
		BgFactors2: calibration.BgFactors2,

		Top3:       calibration.Top3,
		Left3:      calibration.Left3,
		Right3:     calibration.Right3,
		Bottom3:    calibration.Bottom3,
		BgFactors3: calibration.BgFactors3,
	}

	c.IndentedJSON(http.StatusOK, response)
}

// 获取数据库中存储4卡校准数据
func GetChannel4_MFDCG(c *gin.Context) {
	calibration, err := service.GetChannel4Record()
	if err != nil {
		c.JSON(http.StatusOK, &Response{Code: serializer.DatabaseErr, Msg: "数据库操作异常" + err.Error()})
		fmt.Println(err.Error())
		return
	}
	response := service.Channel4RegionService{
		Top1:      calibration.Top1,
		Left1:     calibration.Left1,
		Right1:    calibration.Right1,
		Bottom1:   calibration.Bottom1,
		BgFactors: calibration.BgFactors,

		Top2:       calibration.Top2,
		Left2:      calibration.Left2,
		Right2:     calibration.Right2,
		Bottom2:    calibration.Bottom2,
		BgFactors2: calibration.BgFactors2,

		Top3:       calibration.Top3,
		Left3:      calibration.Left3,
		Right3:     calibration.Right3,
		Bottom3:    calibration.Bottom3,
		BgFactors3: calibration.BgFactors3,

		Top4:       calibration.Top4,
		Left4:      calibration.Left4,
		Right4:     calibration.Right4,
		Bottom4:    calibration.Bottom4,
		BgFactors4: calibration.BgFactors4,
	}

	c.IndentedJSON(http.StatusOK, response)
}
